import os

xdir = os.path.abspath(os.path.join(os.getcwd(), 'x_create_log_dirs.tmp.dir', '2021', '02', '09', '07', '10'))
# print(os.removedirs(xdir))  # OSError: [WinError 145] 目录不是空的


def xremove_dir(xdir):

        xitems = os.listdir(xdir)
        for xi in xitems:
            xi = os.path.join(xdir, xi)
            if os.path.isdir(xi):
                xremove_dir(xi)
            else:
                try:
                    os.remove(xi)
                except Exception as ex:
                    print(f'Deleting file {xi} {ex!r}')
        try:
            os.rmdir(xdir)
        except Exception as ex:
            print(f'Deleting dir {xdir} {ex!r}')


xremove_dir(xdir)
